package com.agent.gateway.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

import java.util.List;

/**
 *  跨域过滤器
 */
@Configuration
public class CrossFilter {
    private static final Logger log= LoggerFactory.getLogger(CrossFilter.class);

    public CrossFilter(){
        log.info("✅ 跨域配置类加载!");
    }

    @Bean
    public CorsWebFilter corsWebFilter(){
        log.info("🔥 跨域配置生效!");
        // 配置源
        UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
        // 配置对象
        CorsConfiguration corsConfiguration=new CorsConfiguration();
        // 配置允许跨域的请求头
        corsConfiguration.addAllowedHeader("*");
        // 配置允许跨域的请求方法
        corsConfiguration.addAllowedMethod("*");
        // 配置允许跨域的请求源
        //todo 根据具体前端地址进行限制
        corsConfiguration.addAllowedOrigin("*"); //配置允许跨域的请求源
        // 配置允许携带cookie
        corsConfiguration.setAllowCredentials(true);
        //允许所有路径请求都应用该策略
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }

}
